import router from '@/router'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css' // 引入进度条样式

const whiteList = ['/login', '/404']

router.beforeEach(async function (to, from, next) {
  NProgress.start()
  //如果有token值，如果当前页在login跳转主页，没有在留在当前页
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.getters.name) {
        await store.dispatch('user/getUserInfo')//没有用户id则获取用户信息
        next(to.path)
      }
      next()

    }
  } else {
    //   //如果跳转的路径包含白名单就跳转，没有则返回登陆页
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
  NProgress.done()//手动关闭进度条
})

router.afterEach(function () {
  NProgress.done()//关闭进度条
})

